﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BO.News;
using ChannelVN.Cached.Base;
using Utilities.Entities;

namespace GUI.Controls.News
{
    public partial class NewsDetail : ControlBase
    {
        public override string CacheConfigName
        {
            get { return "Controls-News-NewsDetail"; }
        }

        public override bool IsCachedChanged(long newsId, string zoneId, long tagId)
        {
            return newsId > 0;
        }

        private const string RelateNewsFormat = "<li><span class=\"liststyle3 sprite fl\"></span><a href=\"{0}\" title=\"{2}\">{1}</a></li>";
        //private const string FacebookLikeButtonFormat = "<iframe src=\"http://www.facebook.com/plugins/like.php?href={0}&amp;layout=standard&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;send=true\" scrolling=\"no\" frameborder=\"0\" style=\"border: none; overflow: hidden;height: 24px; width:440px; padding-right:5px;\" allowtransparency=\"true\"></iframe>";
        public string OriginalUrl { get; set; }
        public int InterviewId { get; set; }

        public bool DataBind(DataTable dt, int interviewId = 0)
        {
            if (interviewId > 0)
            {
                InterviewId = interviewId;
            }
            else
            {
                divFormGiaoLuuTrucTuyen.Visible = false;
            }
            if (dt != null && dt.Rows.Count > 0)
            {
                var r = dt.Rows[0];

                ltrTitle.Text = r[SqlConstants.NewsPublish.Title].ToString();
                ltrSubtitle.Text = 
                    (dt.Columns.Contains(SqlConstants.NewsPublish.SubTitle) 
                    && !string.IsNullOrEmpty(r[SqlConstants.NewsPublish.SubTitle].ToString())
                    && !r[SqlConstants.NewsPublish.SubTitle].ToString().Contains("<a")) 
                    ? string.Format("<p class=\"subtitle\"><strong>{0}</strong></p>", r[SqlConstants.NewsPublish.SubTitle])
                : string.Empty;
                ltrAuthor.Text += !string.IsNullOrEmpty(r[SqlConstants.NewsPublish.Author].ToString())
                                      ? r[SqlConstants.NewsPublish.Author]
                                      : string.Empty;

                var siteId = PresentationUtils.Object2Integer(r[SqlConstants.NewsContent.OriginalId].ToString());
                if (siteId > 0)
                {
                    var site = NewsBO.GetInstance().GetOriginalSiteDetail(siteId);
                    if (site != null)
                    {
                        if (!string.IsNullOrEmpty(ltrAuthor.Text))
                        {
                            ltrAuthor.Text += " - ";
                        }
                        const string authorFormat = "<a href=\"{0}\" rel=\"nofollow\" target=\"_blank\">theo {1}</a>";
                        ltrAuthor.Text += string.Format(authorFormat, site.Url, site.Name);
                    }
                }


                if (!string.IsNullOrEmpty(r[SqlConstants.NewsPublish.Source].ToString()))
                {
                    if (!string.IsNullOrEmpty(ltrAuthor.Text))
                    {
                        ltrAuthor.Text += "/";
                    }

                    ltrAuthor.Text += r[SqlConstants.NewsPublish.Source].ToString().ToLower().Contains("theo ")
                    ? r[SqlConstants.NewsPublish.Source].ToString()
                    : "theo " + r[SqlConstants.NewsPublish.Source];
                }

                ltrSapo.Text = r[SqlConstants.NewsPublish.Sapo].ToString();
                ltrContent.Text = r[SqlConstants.NewsPublish.Content].ToString();
                ltrPublishDate.Text = PresentationUtils.Object2DateTime(r[SqlConstants.NewsPublish.DistributionDate].ToString()).ToString("dd/MM/yyyy HH:mm");


                divKeywords.Visible = LoadKeywords(r[SqlConstants.NewsPublish.TagItem].ToString());

                divRelateNews.Visible = LoadRelateNews(r[SqlConstants.NewsPublish.NewsRelation].ToString());

                OriginalUrl = PresentationUtils.UrlBuilder.BuildAbsoluteUrl(r[SqlConstants.NewsPublish.Url].ToString());
                //ltrFacebookLikeButton.Text = string.Format(FacebookLikeButtonFormat, absoluteUrl);
                return true;
            }
            return false;
        }

        private bool LoadKeywords(string p)
        {
            const char seperator = ';';

            p = p.Trim(seperator);
            if (!string.IsNullOrEmpty(p))
            {
                var sb = new StringBuilder();

                foreach (var item in p.Split(seperator))
                {
                    if (!item.Equals("One_or_more_errors_occurred.", StringComparison.InvariantCultureIgnoreCase))
                    {
                        sb.Append(string.Format("<a href=\"{0}\" title=\"{2}\" itemprop=\"keywords\">{1}</a>", PresentationUtils.UrlBuilder.BuildTagUrl(item), item, PresentationUtils.EnCode(item))).Append(", ");
                    }
                }
                ltrKeywords.Text = sb.ToString().TrimEnd(", ".ToCharArray());

                return true;
            }

            return false;
        }

        private bool LoadRelateNews(string relationNewsAsJson)
        {
            var relationNews = PresentationUtils.JsonToObject<IEnumerable<NewsObject>>(relationNewsAsJson);

            if (relationNews != null)
            {
                var sb = new StringBuilder();
                foreach (var n in relationNews)
                {
                    sb.Append(string.Format(
                            RelateNewsFormat,
                            n.Url,
                            n.Title,
                            PresentationUtils.EnCode(n.Title)
                        ));
                }
                ltrRelateNews.Text = sb.ToString();
                return true;
            }

            return false;
        }
    }
}